package net.scala.chapter3.test

import com.mongodb.BasicDBObject
import net.scala.chapter3.c.MongoClient

/**
 * @author Barudisshu
 */
object QuickTour extends App {

  def client = new MongoClient
  // create a database mydb,and use mydb
  def db = client.db("db")
  for (name <- db.collectionNames) println(name)

  val col = db.readOnlyCollection("test")
  println(col.name)

  val adminCol = db.administrableCollection("test")
  adminCol.drop()

  //更新---------------------------
  val updatableCol = db.updatableCollection("test")

  val doc = new BasicDBObject()
  doc.put("name", "mongoDB")
  doc.put("type", "database")
  doc.put("count", 1)

  val info = new BasicDBObject()
  info.put("x", 203)
  info.put("y", 102)
  doc.put("info", info)
  updatableCol += doc // Add document to collection

  println(updatableCol.findOne)

  updatableCol -= doc // Remove document from collection
  //updatableCol.-=(doc)      // 等价于

  println(updatableCol.findOne)

  for (i <- 1 to 100) updatableCol += new BasicDBObject("i", i)

  val query = new BasicDBObject()
  query.put("i", 71)
  val cursor = col.find(query)
  while (cursor.hasNext) println(cursor.next)


}
